package com.xxmw.transaction.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
/**
 * @author luowenliang
 * @since 2020/2/5 16:48
 */
@Slf4j
public class HmacUtil {

    private static final String HMAC_SHA1 = "HmacSHA1";

    public static final String HMAC_SHA256 = "HmacSHA256";

    public static final String HMAC_SHA512 = "HmacSHA512";

    public static final String HMAC_MD5 = "HmacMD5";

    /**
     * hmacsha256加密
     * @param data 加密内容
     * @param secretKey 秘钥
     * @return
     */
    public static byte[] sha256Encrypt (String data, String secretKey) {
        try {
            SecretKeySpec signKey = new SecretKeySpec(secretKey.getBytes(), HMAC_SHA256);
            Mac mac = Mac.getInstance(HMAC_SHA256);
            mac.init(signKey);
            byte[] rawHmac = mac.doFinal(data.getBytes());
            return rawHmac;
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }

    /**
     * hmacsha512加密
     * @param data 加密内容
     * @param secretKey 秘钥
     * @return
     */
    public static byte[] sha512Encrypt (String data, String secretKey) {
        try {
            SecretKeySpec signKey = new SecretKeySpec(secretKey.getBytes(), HMAC_SHA512);
            Mac mac = Mac.getInstance(HMAC_SHA512);
            mac.init(signKey);
            byte[] rawHmac = mac.doFinal(data.getBytes());
            return rawHmac;
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }

    public static byte[] MD5Encrypt (String data, String secretKey) {
        try {
            SecretKeySpec signKey = new SecretKeySpec(secretKey.getBytes(), HMAC_MD5);
            Mac mac = Mac.getInstance(HMAC_MD5);
            mac.init(signKey);
            byte[] rawHmac = mac.doFinal(data.getBytes());
            return rawHmac;
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }

    /**
     * 火币签名
     * @param data
     * @param secretKey
     * @return
     */
    public static String huobiSign (String data, String secretKey) {
        return Base64.encodeBase64String(sha256Encrypt(data, secretKey));
    }



    /**
     * 币安签名
     * @param data
     * @param secretKey
     * @return
     */
    public static String binanceSign(String data, String secretKey) {
        byte[] bytes = sha256Encrypt(data, secretKey);
        StringBuilder sb = new StringBuilder();
        for (byte item : bytes) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 币夫签名
     * @param data
     * @param secretKey
     * @return
     */
    public static String bitforexSign(String data, String secretKey) {
        byte[] bytes = sha256Encrypt(data, secretKey);
        StringBuilder sb = new StringBuilder();
        for (byte item : bytes) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    }

    /**
     * 币夫签名
     * @param data
     * @param secretKey
     * @return
     */
    public static String bitMexSign(String data, String secretKey) {
        byte[] bytes = sha256Encrypt(data, secretKey);
        StringBuilder sb = new StringBuilder();
        for (byte item : bytes) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    }

    /**
     * 中币签名
     * @param data
     * @param secretKey
     * @return
     */
    public static String ZBSign (String data, String secretKey) {
        byte[] bytes = MD5Encrypt(data, secretKey);
        StringBuilder sb = new StringBuilder();
        for (byte item : bytes) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    }

    /**
     * P网签名
     * @param data
     * @param secretKey
     * @return
     */
    public static String poloniexSign (String data, String secretKey) {
        byte[] bytes = sha512Encrypt(data, secretKey);
        StringBuffer strHexString = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xff & bytes[i]);
            if (hex.length() == 1) {
                strHexString.append('0');
            }
            strHexString.append(hex);
        }
        String result = strHexString.toString();
        return result;
    }

}
